﻿namespace KeRui.Common
{
    using System;
    using System.Configuration;
    using System.Web;
    using System.Windows.Forms;
    using System.Xml;

    public sealed class ConfigHelper
    {
        public static bool GetConfigBool(string key)
        {
            bool flag = false;
            string configString = GetConfigString(key);
            if ((configString != null) && (string.Empty != configString))
            {
                try
                {
                    flag = bool.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return flag;
        }

        public static decimal GetConfigDecimal(string key)
        {
            decimal num = 0M;
            string configString = GetConfigString(key);
            if ((configString != null) && (string.Empty != configString))
            {
                try
                {
                    num = decimal.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return num;
        }

        public static int GetConfigInt(string key)
        {
            int num = 0;
            string configString = GetConfigString(key);
            if ((configString != null) && (string.Empty != configString))
            {
                try
                {
                    num = int.Parse(configString);
                }
                catch (FormatException)
                {
                }
            }
            return num;
        }

        public static int GetConfigInt(string key, int defValue)
        {
            return Utils.StrToInt(GetConfigString(key), defValue);
        }

        public static string GetConfigString(string key)
        {
            string cacheKey = "AppSettings-" + key;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = ConfigurationManager.AppSettings[key];
                    if (cache != null)
                    {
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes(180.0), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return cache.ToString();
        }

        public static void SetAppConfig(string AppKey, string AppValue)
        {
            XmlDocument document = new XmlDocument();
            document.Load(Application.ExecutablePath + ".config");
            XmlNode node = document.SelectSingleNode("//appSettings");
            XmlElement element = (XmlElement) node.SelectSingleNode("//add[@key='" + AppKey + "']");
            if (element != null)
            {
                element.SetAttribute("value", AppValue);
            }
            else
            {
                XmlElement newChild = document.CreateElement("add");
                newChild.SetAttribute("key", AppKey);
                newChild.SetAttribute("value", AppValue);
                node.AppendChild(newChild);
            }
            document.Save(Application.ExecutablePath + ".config");
        }

        public static void SetWebConfig(string key, string strValue)
        {
            string str = "/configuration/appSettings/add[@key='?']";
            XmlDocument document = new XmlDocument();
            document.Load(HttpContext.Current.Server.MapPath("~/web.config"));
            XmlNode node = document.SelectSingleNode(str.Replace("?", key));
            if (node == null)
            {
                throw new ArgumentException("没有找到<add key='" + key + "' value=/>的配置节");
            }
            node.Attributes["value"].InnerText = strValue;
            document.Save(HttpContext.Current.Server.MapPath("~/web.config"));
        }
    }
}

